<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>擅长领域管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true">
				<el-form-item label="咨询师名称">
					<el-input v-model="queryInfo.name" clearable @clear="queryList" placeholder="请输入咨询师名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
				</el-form-item>
			</el-form>
		</el-row>
		<!-- 新增和修改的对话框 -->
		<!-- dialog中绑定close事件，实现对话框右上角叉号的取消功能 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
			<el-form :model="consultspecialty">
				<el-form-item label="咨询师名称" label-width="100px">
					<el-input placeholder="请输入咨询师名称" v-model="consultspecialty.consult.name"></el-input>
				</el-form-item>

				<el-form-item label="擅长领域" label-width="100px">
					<el-input placeholder="请输入擅长领域" v-model="consultspecialty.specialty.name"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="addConsult">确 定</el-button>
			</div>
		</el-dialog>

		<!-- 表格区域 -->
		<el-table ref="multipleTable" :data="showData" border style="width: 100%;"
			:header-cell-style="{'text-align': 'center'}" :cell-style="{'text-align': 'center'}">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="cname" label="咨询师名称">
			</el-table-column>

			<el-table-column prop="sname" label="擅长领域">
			</el-table-column>

			<el-table-column label="操作" width="100">
				<template slot-scope='scope'>
					<el-link type="primary" @click="toEditConsult(scope.row)">编辑</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryInfo.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				// 查询信息对象
				queryInfo: {
					name: '',
					pageNum: 1,
					pageSize: 10
				},
				total: 10,
				// 表格数据
				tableData: [],
				// 显示数据
				showData: [],
				showList: {
					"cname": "",
					"sname": []
				},
				// 对话框标题
				title: '',
				// 是否显示对话框
				dialogFormVisible: false,
				// 咨询师对象 consultspecialty
				consultspecialty: {
					"id": '',
					"consult": {},
					"specialty": {},
				},
			}
		},
		methods: {
			// 分页查询信息的方法
			queryList() {
				this.showData = [];
				// 调用后端接口来获取数据
				this.$http.get('/consultspecialty/find', {
						params: this.queryInfo
					})
					.then(resp => {
						if (resp.data.status == 200) {
							// console.log(resp.data)
							this.tableData = resp.data.data.list;
							this.total = resp.data.data.total;
							// console.log(this.tableData)

							this.showList.cname = this.tableData[0].consult.name
							this.tableData.forEach((item, index) => {
								if (this.showList.cname == item.consult.name) {
									// 名字相等
									this.showList.sname.push(item.specialty.name);
								} else {
									let obj = {
										"cname": this.showList.cname,
										"sname": this.showList.sname
									};
									this.showData.push(obj);
									this.showList.cname = item.consult.name;
									this.showList.sname = [];
									this.showList.sname.push(item.specialty.name);
								}
							});
							let obj = {
								"cname": this.showList.cname,
								"sname": this.showList.sname
							};
							this.showData.push(obj);
							// console.log(this.showData)
						} else {
							this.$message.error('查询失败！')
						}
					})
					.catch((error) => {
						this.$message.error('服务器失联' + error)
					})
			},
			// 进入修改的预览数据页面
			toEditConsult(row) {
				// console.log(row)
				// 赋值
				this.consultspecialty = row;
				// 设置标题
				this.title = "修改咨询师";
				// 开启对话框
				this.dialogFormVisible = true;
			},
			// 修改咨询师
			addConsult() {
				this.setAddress(this.address)
				this.$http.put("/consultspecialty/edit", this.consultspecialty)
					.then(resp => {
						if (resp.data.status == 200) {
							// 消息提示
							this.$message.success('修改成功！');
							// 清空数据
							this.consultspecialty = { // 咨询师对象
								"id": '',
								"consult": "",
								"specialty": "",
							}
							// 关闭对话框
							this.dialogFormVisible = false;
							// 重新查询
							this.queryList()
						} else {
							this.$message.error('接口异常！')
						}
					})
			},
			// 取消对话框
			cancle() {
				this.dialogFormVisible = false;
				this.consultspecialty = { // 咨询师对象
					"id": '',
					"consult": "",
					"specialty": "",
				}
			},
			// 分页
			handleSizeChange(size) {
				//console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size;
				this.queryInfo.pageNum = 1;
				// 重新查询
				this.queryList()
			},
			handleCurrentChange(num) {
				//console.log('页码发生改变');
				this.queryInfo.pageNum = num;
				// 重新查询
				this.queryList();
			},
		},
		mounted() {
			this.queryList();
		},
	}
</script>
<style>
	.el-breadcrumb {
		margin-bottom: 15px;
		padding: 0 0 15px;
		border-bottom: 1px solid #ddd;
	}

	.el-pagination {
		margin-top: 15px;
	}

	.el-link {
		padding: 0 5px;
	}
</style>